﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJ.EventBus.HornedSerpent.Internal;


public class EventBusMessageListener(IEventBusHandlerInvoker eventBusHandlerInvoker, ILogger<IEventBusMessageListener> logger) : IEventBusMessageListener
{
    private readonly IEventBusHandlerInvoker _eventBusHandlerInvoker = eventBusHandlerInvoker;
    private readonly ILogger _logger = logger;

    public async Task<bool> OnReceiveAsync(EventBusMessage message, CancellationToken cancellationToken)
    {
        try
        {
            cancellationToken.ThrowIfCancellationRequested();

            await _eventBusHandlerInvoker.InvokeAsync(message);

            return true;
        }
        catch (Exception ex)
        {
            _logger.LogWarning("[EventBus] MessageListener  {message}", ex.Message);

            return false;
        }
    }
}
